﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyLibary;
using Microsoft.VisualBasic;
using System.IO;
using ReadConfig;
using System.Xml.Linq;
using System.Collections;
using System.Threading;

namespace wfa001
{
    public partial class Form2 : Form
    {
        Ftpable ftp;
        DirectoryInfo downLoad_dirInfo;
        DirectoryInfo upLoad_dirInfo;
        DirectoryInfo errorDir_DownLoad;
        DirectoryInfo errorDir_UpLoad;
        XDocument xdoc;
        IEnumerable<string> ie;
        string[] configFiles;
        static string ftpAddress = "192.168.2.252";
        static string ftpLoginUserName = "update";
        static string ftpLoginPassWord = "update1512";

        public Form2()
        {
            InitializeComponent();

            //---------------------------------------------------
            downLoad_dirInfo = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\download\\");

            if (!downLoad_dirInfo.Exists)
            {
                downLoad_dirInfo.Create();
            }

            upLoad_dirInfo = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\uplaod\\");

            if (!upLoad_dirInfo.Exists)
            {
                upLoad_dirInfo.Create();
            }

            errorDir_DownLoad = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\errorDirDownLoad\\");

            if (!errorDir_DownLoad.Exists)
            {
                errorDir_DownLoad.Create();
            }

            if (!File.Exists(errorDir_DownLoad + "errorContent.txt"))
            {
                File.Create(errorDir_DownLoad + "errorContent.txt");
            }

            errorDir_UpLoad = new DirectoryInfo(Directory.GetCurrentDirectory() + "\\errorDirUpLoad\\");

            if (!errorDir_UpLoad.Exists)
            {
                errorDir_UpLoad.Create();
            }

            if (!File.Exists(errorDir_UpLoad + "errorContent.txt"))
            {
                File.Create(errorDir_UpLoad + "errorContent.txt");
            }
        }
        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            //---------------------------------------------------
            //将服务器上的config文件列表放入窗口列表中。
            //---------------------------------------------------           
            ftp = new Ftpable();
            ftp.FtpUpDown(ftpAddress, ftpLoginUserName, ftpLoginPassWord);
            configFiles = ftp.GetFileList();

            //---------------------------------------------------
            //从指定FTP上下载指定 产品、工序、版本的测试模板
            //---------------------------------------------------

            //---------------------------------------------------
            toolStripProgressBar1.Minimum = 0;

            toolStripProgressBar1.Maximum = configFiles.Length;

            toolStripProgressBar1.Step = 1;

            toolStripProgressBar1.Value = 0;

            toolStripStatusLabel1.Text = "";

            //---------------------------------------------------
            string errorinfo;

            foreach (string file in configFiles)
            {
                toolStripProgressBar1.Value += 1;

                toolStripStatusLabel1.Text = file;

                ftp.Download(ftpAddress, downLoad_dirInfo.FullName, file, out errorinfo);

                Thread.Sleep(100);
            }
        }
        private void btnAppendNode_Click(object sender, EventArgs e)
        {
            DirectoryInfo localDir = new DirectoryInfo(downLoad_dirInfo.FullName);
            FileInfo[] localFiles = localDir.GetFiles();
            
            //---------------------------------------------------
            toolStripProgressBar1.Minimum = 0;

            toolStripProgressBar1.Maximum = localFiles.Length;// configFiles.Length;

            toolStripProgressBar1.Step = 1;

            toolStripProgressBar1.Value = 0;

            toolStripStatusLabel1.Text = "";

            //---------------------------------------------------


            foreach (FileInfo file in localFiles)
            //foreach (string file in configFiles)
            {
                toolStripProgressBar1.Value += 1;

                toolStripStatusLabel1.Text = file.Name; //file;

                try
                {
                    //xdoc = XDocument.Load(downLoad_dirInfo.FullName + file);
                    xdoc = XDocument.Load(downLoad_dirInfo.FullName + file.Name);
                }
                catch (System.Exception ex)
                {
                    //RecordErrorContent(errorDir_DownLoad.FullName, downLoad_dirInfo.FullName, file, ex.Message.ToString());
                    RecordErrorContent(errorDir_DownLoad.FullName, downLoad_dirInfo.FullName, file.Name, ex.Message.ToString());

                    //------------------------------------------------------------------
                    continue;
                }
               
                //----------------------------------------------------
                var queryContent = from productCode in xdoc.Descendants("ProductCode")
                                   select productCode.Value;

                //----------------------------------------------------
                ie = queryContent;

                int icount = ie.Count<string>();

                //----------------------------------------------------
                if (icount == 0)
                {
                    //string[] sProductCode = configFiles[0].Split('_');
                    string[] sProductCode = file.Name.Split('_');

                    XElement xe = new XElement("ProductCode", sProductCode[0]);

                    xdoc.Element("TestingConfig").Add(xe);
                }
                else if (icount == 1)
                {
                    string sContent = xdoc.Element("TestingConfig").Element("ProductCode").Value;

                    if (sContent == "")
                    {
                        //string[] sProductCode = configFiles[0].Split('_');
                        string[] sProductCode = file.Name.Split('_');

                        xdoc.Element("TestingConfig").Element("ProductCode").SetValue(sProductCode[0]);
                    }
                    else if (sContent != "")
                    {

                    }
                }

                //----------------------------------------------------
                xdoc.Save(upLoad_dirInfo + file.Name);
                //xdoc.Save(upLoad_dirInfo + file);
            }
           
        }
        private void btnUpLoad_Click(object sender, EventArgs e)
        {
            DirectoryInfo localDir = new DirectoryInfo(upLoad_dirInfo.FullName);
            FileInfo[] localFiles = localDir.GetFiles();

            //---------------------------------------------------
            toolStripProgressBar1.Minimum = 0;

            toolStripProgressBar1.Maximum = localFiles.Length;// configFiles.Length;

            toolStripProgressBar1.Step = 1;

            toolStripProgressBar1.Value = 0;

            toolStripStatusLabel1.Text = "";

            //---------------------------------------------------
            string errorinfo;

            ftp = new Ftpable();

            ftp.FtpUpDown(ftpAddress, ftpLoginUserName, ftpLoginPassWord);

            foreach (FileInfo file in localFiles)
            //foreach (string file in configFiles)
            {
                toolStripProgressBar1.Value += 1;
                toolStripStatusLabel1.Text = file.Name;// file;

                try
                {
                    //ftp.Upload(upLoad_dirInfo + file, ftpAddress, out errorinfo);
                    ftp.Upload(upLoad_dirInfo + file.Name, ftpAddress, out errorinfo);

                    Thread.Sleep(100);
                }
                catch (System.Exception ex)
                {
                    //RecordErrorContent(errorDir_UpLoad.FullName, upLoad_dirInfo.FullName, file, ex.Message.ToString());
                    RecordErrorContent(errorDir_UpLoad.FullName, upLoad_dirInfo.FullName, file.Name, ex.Message.ToString());

                    //------------------------------------------------------------------
                    continue;
                }

                
                
            }
        }
        private void RecordErrorContent(string errorFilePath, string filePath, string file, string errorContent)
        {
            string sOldErrorConent = "";
            string sNewErrorContent = "";

            //------------------------------------------------------------------
            StreamReader sr = new StreamReader(errorFilePath + "errorContent.txt", Encoding.UTF8);

            sOldErrorConent = sr.ReadToEnd();

            sr.Close();

            //------------------------------------------------------------------
            FileInfo myFile = new FileInfo(errorFilePath + "errorContent.txt");

            StreamWriter sw = myFile.CreateText();

            sNewErrorContent = Environment.NewLine
                + file
                + Environment.NewLine
                + errorContent
                + Environment.NewLine;

            sw.Write(sOldErrorConent + sNewErrorContent);

            sw.Close();

            //------------------------------------------------------------------
            if (!errorContent.Contains("未能找到文件"))
            {
                //------------------------------------------------------------------
                string _sourceFileName = filePath + file;
                string _destFileName = errorFilePath + file;
                File.Copy(_sourceFileName, _destFileName, true);
            }
        }

    }
}
